package br.com.ia.struts.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.enovar.trex.core.model.BaseModel;
import br.com.enovar.trex.struts.action.TRexCRUDBaseAction;
import br.com.enovar.trex.struts.form.TRexBaseForm;
import br.com.ia.business.ItemRegraBusiness;
import br.com.ia.business.OperadorBusiness;
import br.com.ia.business.RegraBusiness;
import br.com.ia.business.ValorBusiness;
import br.com.ia.business.VariavelBusiness;
import br.com.ia.model.ItemRegra;
import br.com.ia.model.Operador;
import br.com.ia.model.Regra;
import br.com.ia.model.Valor;
import br.com.ia.model.Variavel;
import br.com.ia.struts.form.RegraForm;

public class RegraAction extends TRexCRUDBaseAction {

	@Override
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RegraForm regraForm = (RegraForm)form;
		regraForm.setState("insert");
		regraForm.setMethod("insert");
		
		Regra regra = (Regra)copyProperties(regraForm);
		
		ItemRegra itemRegra = new ItemRegra();
		itemRegra.setRegra(regra);
		
		Variavel variavel = new Variavel();
		variavel.setCodigo(regraForm.getVariavel().getCodigo());
		itemRegra.setVariavel(variavel);
		
		Operador operador = new Operador();
		operador.setCodigo(regraForm.getOperador());
		itemRegra.setOperador(operador);
		
		Valor valor = new Valor();
		valor.setCodigo(regraForm.getValor().getCodigo());
		itemRegra.setValor(valor);
		
		ItemRegra conclusao = new ItemRegra();
		conclusao.setRegra(regra);
		
		Variavel variavelConslusao = new Variavel();
		variavelConslusao.setCodigo(regraForm.getCdVariavelConclusao());
		conclusao.setVariavel(variavelConslusao);
		
		Operador operadorConslusao = new Operador();
		operadorConslusao.setCodigo(3);
		conclusao.setOperador(operadorConslusao);
		
		Valor valorConclusao = new Valor();
		valorConclusao.setCodigo(regraForm.getCdValorConclusao());
		conclusao.setValor(valorConclusao);
		
		RegraBusiness business = (RegraBusiness)getBusinessClass(RegraBusiness.class);
		business.insertRegra(regra, itemRegra, conclusao);
		
		
		return search(mapping, regraForm, request, response);
	}

	public ActionForward prepareInserirItemRegra(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RegraForm regraForm = (RegraForm)form;
		regraForm.setState("inserirItemRegra");
		
		ItemRegraBusiness business = (ItemRegraBusiness)getBusinessClass(ItemRegraBusiness.class);
		List<ItemRegra> list = business.bucarItemRegraPorCodigoRegra(regraForm.getCodigo());
		List<ItemRegra> conclusao = business.bucarConclusaoPorCodigoRegra(regraForm.getCodigo());
		
		Integer cdRegraPai = null;
		int index = 0;
		for(ItemRegra i : list){
			if(index ==  list.size() - 1){
				cdRegraPai = i.getCodigo();
			}
			index++;
		}
		
		regraForm.setCdRegraPai(cdRegraPai);
		request.setAttribute("list", list);
		request.setAttribute("conclusao", conclusao);
		
		return mapping.findForward("inserirItemRegra");
	}
	
	public ActionForward inserirItemRegra(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RegraBusiness business = (RegraBusiness)getBusinessClass(RegraBusiness.class);
		VariavelBusiness variavelBusiness = (VariavelBusiness)getBusinessClass(VariavelBusiness.class);
		OperadorBusiness operadorBusiness = (OperadorBusiness)getBusinessClass(OperadorBusiness.class);
		ValorBusiness valorBusiness = (ValorBusiness)getBusinessClass(ValorBusiness.class);
		
		RegraForm regraForm = (RegraForm)form;
		
		Regra regra = (Regra)copyProperties(regraForm);
		
		ItemRegra itemRegra = new ItemRegra();
		regra = (Regra)business.retrieveById(regra.getCodigo());
		itemRegra.setRegra(regra);
		
		Variavel variavel = new Variavel();
		variavel.setCodigo(regraForm.getVariavel().getCodigo());
		variavel = (Variavel)variavelBusiness.retrieveById(variavel.getCodigo());
		itemRegra.setVariavel(variavel);
		
		Operador operador = new Operador();
		operador.setCodigo(regraForm.getOperador());
		operador = (Operador)operadorBusiness.retrieveById(operador.getCodigo());
		itemRegra.setOperador(operador);
		
		Valor valor = new Valor();
		valor.setCodigo(regraForm.getValor().getCodigo());
		valor = (Valor)valorBusiness.retrieveById(valor.getCodigo());
		itemRegra.setValor(valor);
		
		Integer regraPai = regraForm.getCdRegraPai();
		
		ItemRegra itemRegraPai = new ItemRegra();
		itemRegraPai.setCodigo(regraPai);
		itemRegra.setItemRegraPai(itemRegraPai);
		
		itemRegra.setConectivo(regraForm.getConectivo());
		
		
		business.insertItemRegra(itemRegra);
		
		return prepareInserirItemRegra(mapping, regraForm, request, response);
	}
	
	
	public ActionForward deleteItemRegra(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RegraForm regraForm = (RegraForm)form;
		
		Integer codigoItemRegra = regraForm.getCodigoItemRegra();
		
		ItemRegraBusiness business =(ItemRegraBusiness)getBusinessClass(ItemRegraBusiness.class);
		ItemRegra i = new ItemRegra();
		i.setCodigo(codigoItemRegra);
		business.delete(i);
		
		return prepareInserirItemRegra(mapping, regraForm, request, response);
		
	}
	
	
	@Override
	public BaseModel copyProperties(TRexBaseForm form) {
		RegraForm regraForm = (RegraForm) form;
		Regra regra = new Regra();

		regra.setCodigo(regraForm.getCodigo());
		regra.setDescricao(regraForm.getDescricao());
		regra.setOrdem(regraForm.getOrdem());

		return regra;
	}

	@Override
	public void copyProperties(BaseModel model, TRexBaseForm form) {
		RegraForm regraForm = (RegraForm) form;
		Regra regra = (Regra) model;

		regraForm.setCodigo(regra.getCodigo());
		regraForm.setDescricao(regra.getDescricao());
		regraForm.setOrdem(regra.getOrdem());
	}

	@Override
	public Serializable getId(TRexBaseForm form, HttpServletRequest request) {
		RegraForm regraForm = (RegraForm) form;
		return regraForm.getCodigo();
	}

}
